package com.study.zzedu.ui.graduateservice;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.llayjun.colorfultext.ColorfulText;
import com.study.zzedu.R;
import com.study.zzedu.base.BaseMvpActivity;
import com.study.zzedu.entity.ErrorSetItem;
import com.study.zzedu.ui.home.HomeActivity;
import com.study.zzedu.utils.ToastUtil;
import com.study.zzedu.widget.ApplyDegreeDialog;
import com.study.zzedu.widget.ChoiceCourseDialog;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

public class GraduatesServiceActivity extends BaseMvpActivity<GraduateServiceContract.IView, GraduateServicePresenter> implements GraduateServiceContract.IView,View.OnClickListener {
    private RelativeLayout btn_back;
    private TextView tv_degree,tv_banli,tv_title;
    private ColorfulText tv_requests;
    private boolean flag = false;
    View ll_guide,scrow_1;
    ImageButton ibAdminTime,ibLeaveTime;
    EditText et_admin_time,et_leave_time;
    //获取日期格式器对象
    Calendar calendar = Calendar.getInstance(Locale.CHINA);
    DateFormat format =  new SimpleDateFormat("yyyy-MM-dd");

    @Override
    protected int getLayoutID() {
        return R.layout.activity_stu_service;
    }

    @Override
    protected GraduateServicePresenter createPresenter() {
        return new GraduateServicePresenter();
    }

    @Override
    protected GraduateServiceContract.IView createView() {
        return this;
    }

    @SuppressLint("WrongViewCast")
    @Override
    protected void initView() {
        ll_guide = findViewById(R.id.ll_guide);
        scrow_1 = findViewById(R.id.scrow_1);
        btn_back = findViewById(R.id.rl_back);
        tv_degree =findViewById(R.id.tv_degree);
        tv_banli  =findViewById(R.id.tv_banli);
        tv_title  =findViewById(R.id.tv_title);
        et_leave_time = findViewById(R.id.et_leave_time);
        et_admin_time = findViewById(R.id.et_admin_time);
        ibAdminTime = findViewById(R.id.ib_admin_time_date);
        ibLeaveTime = findViewById(R.id.ib_leave_time_date);

        tv_requests = findViewById(R.id.tv_requests);

    }


    private void switchView(boolean flag){
        if(flag){
            ll_guide.setVisibility(View.VISIBLE);
            scrow_1.setVisibility(View.INVISIBLE);
            tv_title.setText(getResources().getString(R.string.stu_service));
        }else {
            ll_guide.setVisibility(View.INVISIBLE);
            scrow_1.setVisibility(View.VISIBLE);
            tv_title.setText(getResources().getString(R.string.go_to_graduate));
        }
    }

    @Override
    protected void initData() {
        flag = true;
        switchView(flag);
        getPresenter().queryApplyConditions(GraduatesServiceActivity.this);
    }

    @Override
    protected void initListener() {
        btn_back.setOnClickListener(this);
        tv_degree.setOnClickListener(this);
        tv_banli.setOnClickListener(this);

        ibAdminTime.setOnClickListener(this);
        ibLeaveTime.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.rl_back:
                if(flag){
                    finish();
                }else {
                    switchView(true);
                    flag = true;
                }
                break;
            case R.id.tv_degree:
                getPresenter().applyXueWei(GraduatesServiceActivity.this);
                break;
            case R.id.tv_banli:
                switchView(false);
                flag = false;
                break;
            case R.id.ib_admin_time_date:
                updateTimeShow(et_admin_time);
                break;
            case R.id.ib_leave_time_date:
                updateTimeShow(et_leave_time);
                break;
        }

    }

    private void updateTimeShow(final TextView v) {
        DatePickerDialog datePickerDialog = new DatePickerDialog(GraduatesServiceActivity.this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                //修改日历控件的年，月，日
                //这里的year,monthOfYear,dayOfMonth的值与DatePickerDialog控件设置的最新值一致
                calendar.set(Calendar.YEAR,year);
                calendar.set(Calendar.MONTH,month);
                calendar.set(Calendar.DAY_OF_MONTH,dayOfMonth);
                v.setText(format.format(calendar.getTime()));
            }
        }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
        datePickerDialog.show();
    }

    @Override
    public void getApplyReturn(String name) {
        ApplyDegreeDialog cd = new ApplyDegreeDialog.Builder(GraduatesServiceActivity.this).create();
        cd.show();
    }
    int textSize= 15;

    @Override
    public void getDataList(String data) {

        try {
            JSONObject jObject = new JSONObject(data);
            JSONArray jArray = jObject.getJSONArray("data");
            for (int i = 0; i < jArray.length();) {
                JSONObject job = jArray.getJSONObject(i);
                if("type_sqsj".equals(job.getString("tpye"))){
                    myAppend(job,"1、申请时间：");
                }else  if("type_nj".equals(job.getString("tpye"))){
                    myAppend(job,"2、申请年级：");
                }else  if("type_bypc".equals(job.getString("tpye"))){
                    myAppend(job,"3、毕业批次：");
                }else  if("type_kccj".equals(job.getString("tpye"))){
                    myAppend(job,"4、课程成绩平均分：");
                }else if("type_xwyycj".equals(job.getString("tpye"))){
                    myAppend(job,"5、学位外语：");
                }else  if("type_lwdbcj".equals(job.getString("tpye"))){
                    myAppend(job,"6、论文答辩：");
                }
                i++;
            }
            if (!(jArray.length() > 0)) {
                ToastUtil.showShortToast("无数据");
                return;
            }
        } catch (Exception e) {
            e.toString();
        }

    }
    private void myAppend(JSONObject job,String data) throws JSONException {
        tv_requests.appendText(data,Color.BLACK,textSize)
                .appendText(getStatus(job.getBoolean("reslut")), Color.RED, textSize)
                .appendText("（当前： ", Color.BLACK, textSize)
                .appendText(TextUtils.isEmpty(job.getString("value"))?" ":job.getString("value"), Color.BLACK, textSize)
                .appendText(" 要求：", Color.BLACK, textSize)
                .appendText(job.getString("target")+")\n", Color.BLACK, textSize);
    }
    private String getStatus(boolean b){
        return b ? "符合" : "不符合";
    }
    private void switchData(){}
    @Override
    public void getData(String data) {

    }

    @Override
    public void showError(String error) {
        if (null != error && !TextUtils.isEmpty(error)) {
            ToastUtil.showShortToast(error);
        }
    }
}
